<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Created by PhpStorm.
 * User: liunwcj
 * Date: 15-4-30
 * Time: 上午9:28
 * http://codeigniter.org.cn/user_guide/libraries/pagination.html
 */
class Message extends CI_Controller
{

    function __construct() {
        parent::__construct();

        $this->load->helper(array('form', 'url'));
        $this->load->model('Message_model');
        $this->load->database();
        $this->load->library('form_validation');

    }
    /*
     *  添加列表页界面
     * */
    function index() {
        $this->page();
    }

    /*
     *  添加视图
     * */
    function add() {
        $data['title'] = '添加留言';
        $this->load->view('public/header', $data);
        $this->load->view('message_edit', $data);
        $this->load->view('public/footer');
    }

    /*
     * 添加留言数据处理
     * */
    function add_message() {
        $data['title'] = '添加留言数据处理';
        $this->form_validation->set_rules('name', 'Name', 'required');
        $this->form_validation->set_rules('url', 'Url', 'required');
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('content', 'Content', 'required');
        if ($this->form_validation->run() == false) {
            $data['title'] = '添加留言错误！';
            $data['error'] = validation_errors();
            $this->load->view('public/header',$data);
            $this->load->view('message_edit',$data);
            $this->load->view('public/footer');
        } else {
            $data = array('name' => $this->input->post('name'), 'url' => $this->input->post('url'), 'title' => $this->input->post('title'), 'content' => $this->input->post('content'), 'date' => date("Y-m-d H:i:s"));
            $this->Message_model->add_message($data);
            redirect(base_url());
        }
    }

    /*编辑视图*/
    function edit($_id) {
        $data['title'] = '编辑留言';
        $result = $this->Message_model->get_message($_id);
        $data['edit_item'] = $result[0];
        $this->load->view('public/header', $data);
        $this->load->view('message_edit', $data);
        $this->load->view('public/footer');
    }

    /*
     * 更新留言内容处理
     * */
    function edit_message() {
        $data['title'] = '内容修改成功！';
        $data = array('id' => $this->input->post('id'), 'name' => $this->input->post('name'), 'url' => $this->input->post('url'), 'title' => $this->input->post('title'), 'content' => $this->input->post('content'), 'date' => date("Y-m-d H:i:s"));
        $this->Message_model->edit_message($data);
        redirect(base_url());
    }

    /*
     * 删除留言内容处理
     * */
    function del($_id) {
        $result = $this->Message_model->del_message($_id);
        redirect(base_url());
    }

    /*
     * 分页
     * */
    function page($offest = 1) {
        $data['title'] = '留言列表';
        //当页显示条数
        $num = 6;
        //当前表的数据位置
        $offest1 = $num * ($offest-1);
        //总条数
        $pages = $this->Message_model->get_all_messages()['num_row'];
//        echo '$pages:'.$pages.'<br/>';
//        echo '$num:'.$num.'<br/>';
//        echo '$offest:'.$offest.'<br/>';
//        echo '$offest1:'.$offest1;
        //输出数据库对应的内容
        $data['item_message'] = $this->Message_model->get_item_message($num,$offest1);
        $data['paging'] = $this->paging($offest, $num,$pages,true);
        $this->load->view('public/header',$data);
        $this->load->view('message_view',$data);
        $this->load->view('public/footer');
    }
    /*
     * 分页UI控件
     * @param int $offest           当前页
     * @param int $num              当页显示条数
     * @param int $pages            总条数
     * @param boolean $dis_pages    显示/隐藏 数字页码
     */
    function paging($offest, $num=4, $pages,$dis_pages = false) {

        //引入分页类库
        $this->load->library('pagination');
        //请求分页的方法，参数
        $config['base_url'] = site_url('message/page');
        //总条数
        $config['total_rows'] = $pages;
        //单页显示条数
        $config['per_page'] = $num;
        //第一页
//        $config['first_link'] = '<<';
        // 最后第一页
//        $config['last_link'] = '>>';
        //当前页，左右两边数字个数
        $config['num_links'] = '1';
        //url当前页面和分页对应
        $config['use_page_numbers'] = true;
        //分页中不显示数字
        $config['display_pages'] = $dis_pages;
        //在整个分页周围围绕一些标签
        $config['full_tag_open'] = '<div style="">';
        $config['full_tag_close'] = '</div>';

        $this->pagination->initialize($config);

        return $this->pagination->create_links();
    }
}
